package com.holly.common_view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.holly.common_view.R;

import java.util.List;

/**
 * 创建人 wzj 创建时间 2016/12/13.
 */

public class BottomNumberPickerDialog extends Dialog {
    private TextView mTvOk;
    private TextView mTvCancel;
    private TextView mTvTitle;
    private NumberPicker mNumberPicker;
    private OnPickerListener mOnPickerListener;

    public BottomNumberPickerDialog(Context context) {
        super(context, R.style.DialogStyle);
        //点击屏幕外关闭
        setCanceledOnTouchOutside(true);

        Context themeContext = getContext();
        //设置为底部
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);

        View view = LayoutInflater.from(themeContext).inflate(R.layout.bottom_number_picker_dialog, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(view, layoutParams);
        initView(view);
    }

    private void initView(View view) {
        mTvOk = (TextView) view.findViewById(R.id.tv_ok);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mTvTitle = (TextView) view.findViewById(R.id.tv_title);
        mNumberPicker = (NumberPicker) view.findViewById(R.id.np);
        mNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        //没数据的时候默认隐藏
        mNumberPicker.setVisibility(View.INVISIBLE);

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mOnPickerListener != null) {
                    mOnPickerListener.onCancelClick(BottomNumberPickerDialog.this);
                }
            }
        });

        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mOnPickerListener != null && mNumberPicker.getVisibility() == View.VISIBLE) {
                    int position = mNumberPicker.getValue();
                    String[] displayedValues = mNumberPicker.getDisplayedValues();
                    if (displayedValues != null && displayedValues.length > position) {
                        mOnPickerListener.onConfirmClick(BottomNumberPickerDialog.this, position, displayedValues[position]);
                    }
                }
            }
        });
    }

    @Override
    public void show() {
        //设置为底部
        Window window = getWindow();
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width;
        window.setAttributes(lp);
        super.show();
    }

    public void setDisplayedValues(String[] array) {
        if (array == null || array.length == 0) {
            mNumberPicker.setVisibility(View.INVISIBLE);
        } else {
            mNumberPicker.setDisplayedValues(null);
            mNumberPicker.setMinValue(0);
            mNumberPicker.setMaxValue(array.length - 1);
            mNumberPicker.setDisplayedValues(array);
            mNumberPicker.setVisibility(View.VISIBLE);
        }
    }

    public void setDisplayedValues(List<String> list) {
        if (list != null) {
            String[] strings = list.toArray(new String[list.size()]);
            setDisplayedValues(strings);
        } else {
            mNumberPicker.setVisibility(View.INVISIBLE);
        }
    }

    public void setOnPickerListener(OnPickerListener onPickerListener) {
        mOnPickerListener = onPickerListener;
    }

    public interface OnPickerListener {
        void onCancelClick(DialogInterface dialog);

        void onConfirmClick(DialogInterface dialog, int position, @Nullable String displayValue);
    }
}
